const db = wx.cloud.database()
import {
  baseActid,
  baseUrl
} from '../../utils/config'
import {
  http,
  res_data
} from '../../utils/http'
Page({
  data: {
    pai: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    lie: ['A', 'B', 'C', 'D', 'E', 'F'],
    zwArr: [],
    myzw: '',
    mask: true,
    conf_flag: true
  },
  async onLoad(options) {
    wx.showLoading({
      title: '加载数据中~'
    })
    const res = await db.collection('info').doc('28ee4e3e60b8a0381f7c1117027eac84').get()
    this.setData({
      title: options.title,
      video_src: res.data.src
    })
  },
  onShow() {
    this.getAlreadyPick()
  },
  getAlreadyPick() {
    let arr = []
    http({
      url: `${baseUrl}getjlinfo?actid=${baseActid}`,
      data: {
        sstime: new Date().getTime()
      }
    }).then(res => {
      const rst = res_data(res.data)
      rst.filter(item => {
        arr.push(item.zwh)
      })
      this.setData({
        zwArr: arr
      })
    })
    http({
      url: `${baseUrl}getjlinfo?actid=${baseActid}`,
      data: {
        phone: wx.getStorageSync('phone'),
        sstime: new Date().getTime()
      }
    }).then(res => {
      let result = JSON.parse(res.data.substr(1, res.data.length - 2))
      if (result.status == 1) {
        this.setData({
          mask: false,
          myzw: result.lists[0].zwh,
          conf_flag: false
        })
      } else {
        return
      }
    })
    wx.hideLoading()
  },
  set_state(e) {
    if (e.currentTarget.dataset.exist) {
      wx.showToast({
        title: '这个位置已经有人了，换一个吧',
        icon: 'none'
      })
      return
    }
    this.setData({
      myzw: e.currentTarget.dataset.id
    })
  },
  confirm() {
    if (!this.data.myzw) {
      wx.showToast({
        title: '还没有选座~',
        icon: 'none'
      })
      return
    }
    http({
      url: `${baseUrl}writelogs?actid=${baseActid}`,
      data: {
        zwh: this.data.myzw,
        phone: wx.getStorageSync('phone'),
        sstime: new Date().getTime()
      }
    }).then(res => {
      const result = JSON.parse(res.data.substr(1, res.data.length - 2))
      if (result.status == 1) {
        wx.showToast({
          title: '选座成功~',
          icon: 'none'
        })
        this.getAlreadyPick()
        this.setData({
          conf_flag: false,
          mask: false,
          myzw: this.data.myzw
        })
      }
    })
  }
})